<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>couchdb.schema</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="couchdb-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="couchdb-module.html">Package&nbsp;couchdb</a> ::
        Module&nbsp;schema
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
      </table>
    </td>
  </tr>
</table>
<!-- ==================== MODULE DESCRIPTION ==================== -->
<h1 class="epydoc">Module schema</h1><p class="nomargin-top"></p>
<p>Mapping from raw JSON data structures to Python objects and vice versa.</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> couchdb <span class="py-keyword">import</span> Server
<span class="py-prompt">&gt;&gt;&gt; </span>server = Server(<span class="py-string">'http://localhost:5984/'</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>db = server.create(<span class="py-string">'python-tests'</span>)</pre>
<p>To define a document schema, you declare a Python class inherited from
<a href="couchdb.schema.Document-class.html" class="link">Document</a>, and add any number of <a href="couchdb.schema.Field-class.html" class="link">Field</a> attributes:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">class</span> <span class="py-defname">Person</span>(Document):
<span class="py-more">... </span>    name = TextField()
<span class="py-more">... </span>    age = IntegerField()
<span class="py-more">... </span>    added = DateTimeField(default=datetime.now)
<span class="py-prompt">&gt;&gt;&gt; </span>person = Person(name=<span class="py-string">'John Doe'</span>, age=42)
<span class="py-prompt">&gt;&gt;&gt; </span>person.store(db) <span class="py-comment">#doctest: +ELLIPSIS</span>
<span class="py-output">&lt;Person ...&gt;</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>person.age
<span class="py-output">42</span></pre>
<p>You can then load the data from the CouchDB server through your <a href="couchdb.schema.Document-class.html" class="link">Document</a>
subclass, and conveniently access all attributes:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>person = Person.load(db, person.id)
<span class="py-prompt">&gt;&gt;&gt; </span>old_rev = person.rev
<span class="py-prompt">&gt;&gt;&gt; </span>person.name
<span class="py-output">u'John Doe'</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>person.age
<span class="py-output">42</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>person.added                <span class="py-comment">#doctest: +ELLIPSIS</span>
<span class="py-output">datetime.datetime(...)</span></pre>
<p>To update a document, simply set the attributes, and then call the <tt class="rst-docutils literal"><span class="pre">store()</span></tt>
method:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>person.name = <span class="py-string">'John R. Doe'</span>
<span class="py-prompt">&gt;&gt;&gt; </span>person.store(db)            <span class="py-comment">#doctest: +ELLIPSIS</span>
<span class="py-output">&lt;Person ...&gt;</span></pre>
<p>If you retrieve the document from the server again, you should be getting the
updated data:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>person = Person.load(db, person.id)
<span class="py-prompt">&gt;&gt;&gt; </span>person.name
<span class="py-output">u'John R. Doe'</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>person.rev != old_rev
<span class="py-output">True</span></pre>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">del</span> server[<span class="py-string">'python-tests'</span>]</pre>

<!-- ==================== CLASSES ==================== -->
<a name="section-Classes"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Classes</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.Field-class.html" class="summary-name">Field</a><br />
      Basic unit for mapping a piece of data between Python and JSON.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.Schema-class.html" class="summary-name">Schema</a>
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.Document-class.html" class="summary-name">Document</a>
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.TextField-class.html" class="summary-name">TextField</a><br />
      Schema field for string values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.FloatField-class.html" class="summary-name">FloatField</a><br />
      Schema field for float values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.IntegerField-class.html" class="summary-name">IntegerField</a><br />
      Schema field for integer values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.LongField-class.html" class="summary-name">LongField</a><br />
      Schema field for long integer values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.BooleanField-class.html" class="summary-name">BooleanField</a><br />
      Schema field for boolean values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.DecimalField-class.html" class="summary-name">DecimalField</a><br />
      Schema field for decimal values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.DateField-class.html" class="summary-name">DateField</a><br />
      Schema field for storing dates.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.DateTimeField-class.html" class="summary-name">DateTimeField</a><br />
      Schema field for storing date/time values.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.TimeField-class.html" class="summary-name">TimeField</a><br />
      Schema field for storing times.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.DictField-class.html" class="summary-name">DictField</a><br />
      Field type for nested dictionaries.
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a href="couchdb.schema.ListField-class.html" class="summary-name">ListField</a><br />
      Field type for sequences of other fields.
    </td>
  </tr>
</table>
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="couchdb-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Mon Dec 14 15:25:33 2009
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
